<template>
    <div class="dahezi">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item >🏠</el-breadcrumb-item>
            <el-breadcrumb-item>病友论坛</el-breadcrumb-item>
        </el-breadcrumb>
        <el-button   @click="dianji" type="success" >发帖</el-button>
        <div class="hezi">
            <div class="hezi2">
                <div class="hezi3">
                    <div class="aaa">
                        <!--                  内容-->
<!--                        <div class="yierhang">-->
                            <div  v-for="(p,index) in tiezii" :key="index">
                                <!--            帖子标题-->
                                <!--                一条帖子的框框，从这里开始v-for遍历-->
                                <div class="rediang1" >
                                    <div id="rcorners1" >

                                        {{p.classificationName}}
                                    </div>
                                    <router-link :to="{name:'tiezixiangqing',
                                        query:{id:p.id,
                                        type:p.classificationName,
                                        content:p.content,
                                        time:p.time
                                        }}">
                                        <div class="neirong">
                                            <div v-html="p.content.length > 100 ? p.content.slice(0, 100) + '...' :p.content" />
                                        </div>
                                    </router-link>
                                    <div class="huifucisu">
                                        回复({{p.count}})
                                    </div>

                                </div>
                            </div>
                        <el-row style="font-size: 30px">
                            <!--     分页     -->
                            <el-pagination
                                    v-show="total>0"
                                    :page-size="pageSize"
                                    :pager-count="11"
                                    :current-page.sync="pageNum"
                                    :total="total"
                                    @current-change="tiezi"
                            >
                            </el-pagination>

                        </el-row>
<!--                        </div>-->
                    </div>
                </div>
            </div>
        </div>

        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="发帖人:" >
                    <el-tag type="info" effect="plain">{{form.userName}}</el-tag>
                    <!--                        <el-input v-model="form.userName" placeholder="请输入文章笔名" />-->
                </el-form-item>
                帖子分类:
                <el-radio-group v-model="form.classificationId" v-for="(p,index) in fengleibie" :key="index">
                    <el-radio :label="p.id">{{p.className}}</el-radio>
                </el-radio-group>
                <el-form-item style="margin-top: 20px" label="帖子内容:">
                    <editor v-model="form.content" :min-height="200"/>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="tijiao">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog
                title="发布成功，快邀请医生来回答吧！！"
                :visible.sync="dialogVisible"
                width="680px">
            <div class="dalog">
                <div class="doctouavatar">
                    <img :src="doctorList[0].avatar"  style="width:200px; height:200px"/>
                    <div class="yaoqinganniu">
                        <el-button type="primary"  @click="invitation(0)">邀请回答</el-button>
                    </div>
                </div>
                <div class="doctorjiesao">
                    {{doctorList[0].username}}医生<br/>
                    已回复过<br/>
                    {{doctorList[0].responseTopicCount}}次了
                </div>
                <div class="doctouavatar">
                    <img src="doctorList[1].avatar"  style="width:200px; height:200px"/>
                    <div class="yaoqinganniu">
                        <el-button type="primary"  @click="invitation(1)">邀请回答</el-button>
                    </div>
                </div>
                <div class="doctorjiesao">
                    {{doctorList[0].username}}医生<br/>
                    已回复过<br/>
                    {{doctorList[0].responseTopicCount}}次了
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <div style="color: black; margin-left: 20px">换一批</div>
                <el-button type="primary"  class="el-icon-shuaxin" @click="changDoctor"></el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import marked from "marked";
    import hljs from "highlight.js";
    import "highlight.js/styles/monokai-sublime.css";
    import Editor from '@/components/Editor';


    export default {
        name: "luntang",
        components: { Editor },
        data(){
            return{
                shiyisheng:'',
                form:{
                    userName:'',
                    userId:'',
                    content:'',
                    classificationId:''
                },
                rules:{},
                tiezii:[],
                fengleibie:[],
                open:false,
                dialogVisible: false,
                doctorList: [
                    {
                        "doctorId": '',
                        "username": '',
                        "responseTopicCount": 9,
                        "avatar": ''
                    }
                ],
                page: 1,
                title: '',
                meaasgeDetail:{
                    "bid": 0,  //帖子id
                    "detail": "邀请",
                    "status": 1,  // 1表示未读
                    "userId": 0   //医生用户id
                },
                newTopicId: '',
                pageSize: 10,
                pageNum: 1,
                total: 0,
            }
        },
        mounted() {

            //初始化

            this.tiezi()
            marked.setOptions({
                renderer: new marked.Renderer(),
                highlight: function (code) {
                    return hljs.highlightAuto(code).value;
                },
                pedantic: false,
                gfm: true,
                tables: true,
                breaks: true,
                sanitize: false,
                smartLists: true,
                smartypants: false,
                xhtml: true,
            });
        },
        created() {
            this.tiezi();
            this.getDoctors();
        },
        methods:{
            invitation(i){
                this.meaasgeDetail.bid = this.newTopicId;
                this.meaasgeDetail.userId = this.doctorList[i].doctorId;
                console.log(this.meaasgeDetail)
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                this.$http.post('/message/sendDoctorMessage',this.meaasgeDetail).then(res => {
                     if (res.data.code == 200){
                            this.$message({showClose: true, message: "邀请成功！", type: "success"})
                     }
                })
            },
            // 掉医生信息接口
            changDoctor(){
                this.getDoctors();
                this.page = this.page+1;
            },

            async getuserlll(){
                const {data:jieguo}=await this.$http.get('user/info',{headers:{
                        'Authorization':window.sessionStorage.getItem('token')
                    }})
                jieguo.data.basicUserInformation.id
                this.shiyisheng=jieguo.data.userRole
                this.form.userName=jieguo.data.basicUserInformation.username
                this.form.userId=jieguo.data.basicUserInformation.id
                this.basicUserInformation=jieguo.data.basicUserInformation
                // console.log( this.shiyisheng)
            },
            async tiezi(){
                const {data:jieguo}=await this.$http.get('topic/list?pageNum='+this.pageNum)
                this.tiezii=jieguo.data.data
                this.total = jieguo.data.total
            },
           async fenglei(){
                const {data:jieguo}=await this.$http.get('/classification/list',{headers:{
                        'Authorization':window.sessionStorage.getItem('token')
                    }})
               this.fengleibie=jieguo.data
            },
            dianji(){
                this.getuserlll();
                if(this.shiyisheng == '')
                    this.open = false
                else{
                    this.fenglei();
                    this.open=true;
                    this.tiezifenlei();
                }

            },
            getDoctors() {
                this.$http.get('/user/getDoctors?page=' + this.page).then(res => {
                    console.log(res.data);
                    this.doctorList = res.data.data.data;
                    console.log(this.doctorList);

                })
            },
            tijiao() {
                if (!this.form.classificationId) {
                    this.$message('请选择分类');
                    return false
                }
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                this.$http.post('topic', this.form).then(response => {
                    this.newTopicId = response.data.data.topicId;
                    console.log(response);
                    if (response.data.code === 200) {
                        this.open = false;
                        this.tiezi()
                        if(this.form.classificationId==2){
                            this.dialogVisible=true;
                        }

                    }
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form.content=''
                this.form.classificationId=''
            }
        }
    }
</script>

<style >
    .el-icon-shuaxin{

    background-image: url('../../../assets/css/shuaxin.jpg');

        background-size:100% 100%;

        width:20px;

        height:20px;

    }

    .hezi{
        width: 80%;
        min-height: 900px;
        height: auto!important;
        display: flex;
        justify-content:center;
        align-items:center;

    }
    .hezi2{
        width: 100%;
        background-color: white;
        min-height: 850px;
        height: auto!important;
        margin-top: 3px;
        display: flex;
        /*justify-content:center;*/
    }
    .hezi3{
        width: 100%;
    }

    .dahezi{
        padding-top: 3px;
        background-color: #fffacd;
        min-height: 1000px;
        height: auto!important;
    }
    .aaa{
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        overflow: scroll;
        min-height: 850px;
        width: 100%;

    }
    .yierhang{
        width: 800px;
        height: 100px;
        text-align: center;
        margin-left: 200px;
        margin-top: 20px;
        background: #FFFFE0;
        border: #FFFACD solid 2px;
    }
    .jinrirediang1{
        height: 100px;
        width: 40%;
        margin-bottom:25px;
    }
    .rediang1{
        border: #FFFACD solid 2px;
        background: #FFFFE0;
        margin-top: 20px;
        margin-left: 15%;
        width: 70%;
        height: 150px;
        overflow: hidden;
        text-overflow: ellipsis;
        color: #111111;
    }
    .neirong{
        height: 110px;
    }
    .huifucisu{
        height: 50px;
        width: 80px;
        float: right;
        color: black;
    }
    #rcorners1 {
        border-radius: 100px;
        background: white;
        padding: 20px;
        width: 75px;
        height: 0px;
        float: right;
        text-align: center;
        color: black;
        font-size: 18px;
        border: #EAEDF1 solid 1px;
    }
    .el-dialog{
        background-color: #FFFFE0;
    }
    .dalog{
        display: flex;
        width: 650px;
        height: 250px;
        /*border: #EEE8AA solid 2px;*/
    }
    .doctouavatar{
        width: 200px;
        height: 200px;
        /*border: #888888 solid 2px;*/
    }
    .doctorjiesao{
        width: 150px;
        height: 150px;
        /*border: black solid 2px;*/
        margin-top: 50px;
        font-size: 20px;
        text-align: center;
    }
    .yaoqinganniu{
        width: 200px;
        height: 20px;
        float: bottom;
        /*border: black solid 2px;*/
    }
</style>
